package com.wl.controller;

import com.wl.exception.BusinessException;
import com.wl.model.NewsInfo;
import com.wl.service.CategoryService;
import com.wl.service.NewsInfoService;
import com.wl.utils.PageUtil;
import com.wl.vo.NewsInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * @author lb
 * @date 2018/4/17 18:01
 */
@RequestMapping("/newsInfo")
@Controller
public class NewsInfoController {

    @Autowired
    NewsInfoService newsInfoService;

    @Autowired
    CategoryService categoryService;

    @Autowired
    UserController userController;

    /***
     * 获取所有新闻并分页
     * @param pageUtil pageUtil
     * */
    @GetMapping("/getAllNewsInfo")
    public ModelAndView getAllNewsInfo(PageUtil pageUtil, HttpServletRequest request) {
        PageUtil newPageUtil = this.getPageUtil(pageUtil);
        request.getSession().setAttribute("newsInfoList", newsInfoService.getLimitNewsInfo(newPageUtil));
        request.getSession().setAttribute("pageUtil", newPageUtil);
//        request.getSession().setAttribute("category", categoryService.list(null,null));
        final ModelAndView modelAndView = new ModelAndView("newsManage");
        return modelAndView;
    }

    /***
     * 条件查询新闻
     * @param newsInfo newsInfo
     * */
    @PostMapping("/findByNewsInfo")
    public ModelAndView findByNewsInfo(NewsInfo newsInfo, HttpServletRequest request) throws UnsupportedEncodingException {
        request.getSession().setAttribute("newsInfoList", newsInfoService.findByNewsInfo(newsInfo));
        final ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("newsManage");
        return modelAndView;
    }

    /***
     * 删除新闻
     * @param newsId newsId
     * */
    @GetMapping("/deleteNewsInfo")
    public ModelAndView deleteNewsInfo(long newsId,PageUtil pageUtil, HttpServletRequest request) throws UnsupportedEncodingException {
        newsInfoService.deleteNewsInfo(newsId);
        ModelAndView allNewsInfo = this.getAllNewsInfo(pageUtil, request);
        return allNewsInfo;
    }

    /***
     * 添加新闻
     * @param newsInfo newsInfo
     * */
    @PostMapping("/addNewsInfo")
    public ModelAndView addNewsInfo(NewsInfo newsInfo,PageUtil pageUtil, HttpServletRequest request, MultipartFile file) throws IOException {
        if(file != null){
            final String imageFileName = userController.saveImage(file);
            newsInfo.setImage(imageFileName);
        }
        newsInfoService.addNewsInfo(newsInfo);
        ModelAndView allNewsInfo = this.getAllNewsInfo(pageUtil, request);
        return allNewsInfo;
    }

    /***
     * 修改新闻
     * @param newsInfo newsInfo
     * */
    @PostMapping("/updateNewsInfo")
    public ModelAndView updateNewsInfo(NewsInfo newsInfo, PageUtil pageUtil, HttpServletRequest request, MultipartFile file) throws IOException {

        if(file != null){
            final String imageFileName = userController.saveImage(file);
            newsInfo.setImage(imageFileName);
        }

        newsInfoService.updateNewsInfo(newsInfo);
        ModelAndView allNewsInfo = this.getAllNewsInfo(pageUtil, request);
        return allNewsInfo;
    }

    /***
     * 获取所有新闻
     *
     * */
    @GetMapping("/allNewsInfo")
    public ModelAndView allNewsInfo(HttpServletRequest request){
        final ModelAndView modelAndView = new ModelAndView("homePage");
        request.setAttribute("newsInfoList", newsInfoService.allNewsInfo());
        request.getSession().setAttribute("newsCategoryList", newsInfoService.allNewsCategory());
        return modelAndView;
    }

    @GetMapping("/getNewsInfo")
    public ModelAndView getNewsInfo(long newsId, HttpServletRequest request) throws UnsupportedEncodingException {
        request.getSession().setAttribute("newsInfo", newsInfoService.getNewsInfo(newsId));
        request.getSession().setAttribute("category", categoryService.list(null,null));
        final ModelAndView modelAndView = new ModelAndView("addAndUpdateNewsInfo");
        modelAndView.addObject("isUpdate","update");
        return modelAndView;
    }

    @GetMapping("/getNewsInfoDetail")
    public ModelAndView getNewsInfo(long newsId) throws UnsupportedEncodingException {
        final NewsInfoVo newsInfoVo = newsInfoService.newsInfoDetail(newsId);
        final ModelAndView modelAndView = new ModelAndView("newsDetail");
        modelAndView.addObject("vo",newsInfoVo);
        return modelAndView;
    }
    
    private PageUtil getPageUtil (PageUtil pageUtil){
        pageUtil.getPageCount(newsInfoService.getCountNewsInfo());
        if (pageUtil.getCurrentPage() == 0){
            pageUtil.setCurrentPage(1);
        } else if (pageUtil.getCurrentPage() > pageUtil.getTotalPage()) {
            pageUtil.setCurrentPage(pageUtil.getCurrentPage()-1);
        }
        pageUtil.setIndexPage((pageUtil.getCurrentPage()-1) * pageUtil.getPageSize());
        return pageUtil;
    }

}
